import axios from 'axios'
 import qs from 'qs'
 
 // 按现有项目风格：使用 Controller 路径 + x-www-form-urlencoded
 // 直接透传 camelCase 字段（与其它视图保持一致）
 export function saveAddress(payload) {
  // 将前端 camelCase 字段映射为后端表结构的 snake_case
  const mapped = {
    id: payload.id,
    userId: payload.userId,
    contactName: payload.contactName,
    gender: payload.gender ?? payload.contactSex, // 兼容两种命名
    phone: payload.phone ?? payload.contactTel,
    detail: payload.detail ?? payload.address,
    isDefault: payload.isDefault ?? 0,
  }
  return axios.post('/address', mapped)
 }
 
// 抽离：根据用户ID查询地址列表
export function listDeliveryAddressByUserId(userId) {
  return axios.get(
    'address/user/' + userId,
    {  }
  )
}

// 修改地址（参考新增方法）
export function updateAddress(payload) {
  const mapped = {
    id: payload.id,
    userId: payload.userId,
    contactName: payload.contactName,
    gender: payload.gender ?? payload.contactSex,
    phone: payload.phone ?? payload.contactTel,
    detail: payload.detail ?? payload.address,
    isDefault: payload.isDefault ?? 0,
  }
return axios.put(`/address/${payload.id}`, mapped)
}

// 删除地址（参考新增方法）
export function removeAddress(id) {
  return axios.delete(`/address/${id}`)
}

// 获取单个地址详情
export function getAddressById(id) {
  return axios.get(`/address/${id}`)
}

 
